package com.xxx;

import com.xxx.codec.RpcDecoder;
import com.xxx.adapter.ServiceHandlerAdapter;
import com.xxx.request.RpcRequest;
import com.xxx.serializer.FastJsonSerializer;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.string.StringEncoder;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

@SpringBootApplication
public class ServerBootstrap implements CommandLineRunner, ApplicationContextAware {

    private final static String HOST = "127.0.0.1";

    private final static int PORT = 8990;

    private ApplicationContext applicationContext;

    public static void main(String[] args) {

        SpringApplication.run(ServerBootstrap.class, args);

    }


    @Override
    public void run(String... args) throws Exception {
        startServer(HOST, PORT);
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void startServer(String hostName, int port) {

        NioEventLoopGroup bossGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        io.netty.bootstrap.ServerBootstrap serverBootstrap = new io.netty.bootstrap.ServerBootstrap();
        serverBootstrap.group(bossGroup, workerGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        //解决粘包/半包,根据消息长度自动拆包

                        ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(65535, 0, 2, 0, 2));

                        pipeline.addLast(new StringEncoder());
                        pipeline.addLast(new RpcDecoder(RpcRequest.class, new FastJsonSerializer()));

                        //解决粘包/半包问题附加消息长度在消息头部
                        ch.pipeline().addLast(new LengthFieldPrepender(2));
                        pipeline.addLast(new ServiceHandlerAdapter(applicationContext));

                    }
                });
        try {
            //绑定端口,同步等待成功

            serverBootstrap.bind(hostName, port).sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        System.out.println("-------------------------Netty server has started at port :" + port+" --------------------------");

    }


}
